package com.example.demo1.Dao;


import com.example.demo1.pojo.Goods;
import lombok.var;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class GoodsDao {
    private Map<String,Goods> goodsMap;
    GoodsDao()
    {
        this.goodsMap = new HashMap<>();
    }
    public List<Goods> loadAllGoods()
    {
        List<Goods> lis = new ArrayList<>();
        for(var a : goodsMap.entrySet())
        {
            lis.add(a.getValue());
        }

        return lis;
    }
    public boolean deleteGoodsByName(String name)
    {
        if(goodsMap.containsKey(name))
        {
            goodsMap.remove(name);
            return true;
        }
        return false;
    }

    public boolean addGoods(Goods goods)
    {
        if(goodsMap.containsKey(goods.getGoodsName()))
        {
            return false;
        }
        goodsMap.put(goods.getGoodsName(),goods);
        System.out.println("成功添加" + goods.getGoodsName());
        return true;
    }

    public boolean updateGoods(String name,String price)
    {
        if(goodsMap.containsKey(name))
        {
            goodsMap.get(name).setGoodsPrice(price);
            System.out.println("成功添加update" + name);
            return true;
        }
        System.out.println(name + "不存在");
        return false;
    }
}
